/**
 * 流程发起
 * @param  {[type]} processObject [{
 *     title:"测试流程001",
 *     businessId:"项目ID等",
 *     procDefKey:"qingjia"
 * }]
 * @return {[type]}               [description]
 */

layui.define(['configureCom'], function (exports) {
    var configureCom = layui.configureCom;

    /*
    startProcess({
        "title":"测试流程",
        "businessId":"1234567890",
        "procDefKey":"qingjia"
    })*/
    function startProcess(processObject) {
        configureCom.postAjax("/api/act/ctx/save", processObject).then(function (res) {
            next({
                "procDefKey": processObject.procDefKey,
                "poId": res.data.poId
            });
        });
    }


    /**
     * [processOutputCtx description]
     * @type {Object}
     */
// var processOutputCtx = {};

    /**
     * [processInputCtx description]
     * @type {Object}
     */
//var processInputCtx = {}
//var buildProcessInputCtx = {};
// var getParticipantsVariables = {};
// var getOpinion = {};
    /**
     * 获取下一步，打开下一步页面
     * @param  {[type]}   processCtxParameter [description]
     * @return {Function}                     [description]
     */
    function next(processCtxParameter) {
        configureCom.postAjax("/api/act/ctx/next", processCtxParameter).then(function (res) {
            top.processOutputCtx = res.data;
            top.layer.open({
                type: 2 //此处以iframe举例
                , title: '当前阶段：'+res.data.taskName
                , area: ['900px', '500px']
                , shade: [0.3, '#000']
                , shadeClose:true
                , maxmin: true
                , content: "/templates/act/next-xl.html"
                , btn: ['提交']
                , yes: function () {
                    //获取选节点和人的 variables
                    var participantsVariables = top.getParticipantsVariables();
                    participantsVariables.forEach(function (p) {
                        top.processOutputCtx.variables.forEach(function (v, index) {
                            if (p.name === v.name) {
                                top.processOutputCtx.variables.splice(index, 1)
                            }
                        });
                    });
                    commit({
                        "taskId": top.processOutputCtx.taskId,
                        "procDefId": top.processOutputCtx.procDefId,
                        "poId": top.processOutputCtx.poId,
                        "variables": participantsVariables.concat(top.processOutputCtx.variables),
                        "opinion": top.getOpinion()
                    });
                }
                , zIndex: layer.zIndex
            });
        });
    }

    /*
    使用next提交流程
    next({
        "taskId":taskInfo.taskId,
        "poId":taskInfo.poId,
        "variables":[
            {"name":"pass","value":data.field.pass}
        ]
    },{
        "pass":data.field.pass,
        "passName":$('input:radio:checked').attr('title'),
        "comments":data.field.comments
    });*/


    /**
     * 启动流程
     * @param  {[type]} processInputCtx [description] :
     *  {
		"taskId"	:processOutputCtx.taskId,
		"procDefId"	:processOutputCtx.procDefId,
		"poId"		:processOutputCtx.poId,
		"variables"	:participantsVariables.concat(processOutputCtx.variables),
		"opinion"	:opinion,
	}

     * @return {[type]}                 [description]
     */
    function commit(processInputCtx) {
        configureCom.postAjax("/api/act/ctx/commit", processInputCtx).then(function (res) {
            if (res.msg == 'success') {
                top.layer.alert('提交成功!');
            }
        });
    }

    var process = {
        startProcess: function (processObject) {
            configureCom.postAjax("/api/act/ctx/save", processObject).then(function (res) {
                process.next({
                    "procDefKey": processObject.procDefKey,
                    "poId": res.data.poId
                });
            });
        },
        /**
         * 启动流程
         * @param  {[type]} processInputCtx [description] :
         *  {
		"taskId"	:processOutputCtx.taskId,
		"procDefId"	:processOutputCtx.procDefId,
		"poId"		:processOutputCtx.poId,
		"variables"	:participantsVariables.concat(processOutputCtx.variables),
		"opinion"	:opinion,
	}

         * @return {[type]}                 [description]
         */
        commit: function (processInputCtx) {
            configureCom.postAjax("/api/act/ctx/commit", processInputCtx).then(function (res) {
                if (res.code == 0) {
                    top.layer.alert('提交成功!', function (index, layero) {
                        // top.layer.closeAll();
                        top.layer.close(index);
                        configureCom.closeWind();
                    });
                }
                try{
                    //非工作流页面发起的流程提交以后刷新表单
                    tableRefresh();
                }catch (e){}
            });
        },
        /**
         * 获取下一步，打开下一步页面
         * @param  {[type]}   processCtxParameter [description]
         * @return {Function}                     [description]
         */
        next: function (processCtxParameter) {
            configureCom.postAjax("/api/act/ctx/next", processCtxParameter).then(function (res) {
                top.processOutputCtx = res.data;
                top.layer.open({
                    type: 2 //此处以iframe举例
                    //, title: '下一步：'
                    , title:'当前阶段：'+ res.data.taskName
                    , area: ['850px', '500px']
                    , shade: [0.3, '#000']
                    , shadeClose:true
                    , maxmin: true
                    , content: "/templates/act/next-xl.html"
                    , btn: ['提交']
                    , yes: function (index, layero) {
                        //获取选节点和人的 variables
                        var participantsVariables = top.getParticipantsVariables();
                        participantsVariables.forEach(function (p) {
                            top.processOutputCtx.variables.forEach(function (v, index) {
                                if (p.name === v.name) {
                                    top.processOutputCtx.variables.splice(index, 1)
                                }
                            });
                        });
                        top.layer.close(index);
                        process.commit({
                            "taskId": top.processOutputCtx.taskId,
                            "procDefId": top.processOutputCtx.procDefId,
                            "poId": top.processOutputCtx.poId,
                            "variables": participantsVariables.concat(top.processOutputCtx.variables),
                            "opinion": top.getOpinion()
                        });
                    }
                    , zIndex: layer.zIndex
                });
            });
        }
    };
    exports('process', process);
});

